package org.yu.team2023.flashdays;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.client.ForgeRenderTypes;
import net.minecraftforge.client.RenderTypeGroup;
import net.minecraftforge.client.model.generators.ModelFile;
import net.minecraftforge.client.model.generators.loaders.ItemLayerModelBuilder;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.yu.team2023.flashdays.item.TreasureBox;
import org.yu.team2023.flashdays.item.TreasureCurios;
import org.yu.team2023.flashdays.item.UnidentifiedTreasure;
import org.yu.team2023.flashdays.registries.SimpleItemModelProvider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;


@EqualsAndHashCode
@ToString
public class TreasureSets {
    public static final Map<String, TreasureSets> TREASURE_SETS_MAP = new HashMap<>();
    @Getter
    String setName;
    @Getter
    final RegistryObject<TreasureCurios> treasure4, treasure1, treasure2, treasure3;

    @Getter
    final Map<TreasureQuality, RegistryObject<UnidentifiedTreasure>> unidentifieds;
    @Getter
    final RegistryObject<TreasureBox> box;
    @Getter
    final List<RegistryObject<TreasureCurios>> curiosList;

    private TreasureSets(String name, String ch1, String ch2, String ch3, String ch4, String setName, String setNameCn, String setDesc, String setDesc4, SetTiger tiger) {
        this.setName = setName;
        this.treasure1 = FlashDays.getSimpleReg().item(name + 1, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 1)).lang(ch1).model(this::generatorModel).build(ModItemTags.CURIOS_TREASURE_1, ModItemTags.CURIOS_TREASURES);
        this.treasure2 = FlashDays.getSimpleReg().item(name + 2, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 2)).lang(ch2).model(this::generatorModel).build(ModItemTags.CURIOS_TREASURE_2, ModItemTags.CURIOS_TREASURES);
        this.treasure3 = FlashDays.getSimpleReg().item(name + 3, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 3)).lang(ch3).model(this::generatorModel).build(ModItemTags.CURIOS_TREASURE_3, ModItemTags.CURIOS_TREASURES);
        this.treasure4 = FlashDays.getSimpleReg().item(name + 4, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 4)).lang(ch4).model(this::generatorModel).build(ModItemTags.CURIOS_TREASURE_4, ModItemTags.CURIOS_TREASURES);
        TREASURE_SETS_MAP.put(setName, this);
        this.box = FlashDays.getSimpleReg().item("box_of_" + name, (p) -> new TreasureBox(p.stacksTo(64).fireResistant(), setName)).lang("珍宝之匣【" + setNameCn + "】").copyModel(() -> Items.NETHER_STAR).build();
        FlashDays.getSimpleReg().lang("tip." + this.setName + ".desc", setDesc);
        FlashDays.getSimpleReg().lang("tip." + this.setName + ".desc4", setDesc4);
        FlashDays.getSimpleReg().lang("tip." + this.setName + ".nameDesc", setNameCn);
        this.curiosList = ImmutableList.of(treasure1, treasure2, treasure3, treasure4);

        this.unidentifieds = ImmutableMap.of(
                TreasureQuality.COMMON, FlashDays.getSimpleReg().item("unidentified_" + name + "0", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.COMMON)).lang("未鉴定的【"+setNameCn+"】-普通").model(this::generatorModelUnidentified).build(),
                TreasureQuality.UNCOMMON, FlashDays.getSimpleReg().item("unidentified_" + name + "1", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.UNCOMMON)).lang("未鉴定的【"+setNameCn+"】-一般").model(this::generatorModelUnidentified).build(),
                TreasureQuality.RARE, FlashDays.getSimpleReg().item("unidentified_" + name + "2", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.RARE)).lang("未鉴定的【"+setNameCn+"】-稀有").model(this::generatorModelUnidentified).build(),
                TreasureQuality.EPIC, FlashDays.getSimpleReg().item("unidentified_" + name + "3", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.EPIC)).lang("未鉴定的【"+setNameCn+"】-史诗").model(this::generatorModelUnidentified).build()
        );
        SetTiger.register(setName, tiger);
    }

    private TreasureSets(String name, String ch1, String ch2, String ch3, String ch4, String setName, String setNameCn, String setDesc, String setDesc4, SetTiger tiger, boolean simpleModel) {
        this.setName = setName;
        this.treasure1 = FlashDays.getSimpleReg().item(name + 1, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 1)).lang(ch1).simpleModel().build(ModItemTags.CURIOS_TREASURE_1, ModItemTags.CURIOS_TREASURES);
        this.treasure2 = FlashDays.getSimpleReg().item(name + 2, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 2)).lang(ch2).simpleModel().build(ModItemTags.CURIOS_TREASURE_2, ModItemTags.CURIOS_TREASURES);
        this.treasure3 = FlashDays.getSimpleReg().item(name + 3, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 3)).lang(ch3).simpleModel().build(ModItemTags.CURIOS_TREASURE_3, ModItemTags.CURIOS_TREASURES);
        this.treasure4 = FlashDays.getSimpleReg().item(name + 4, (p) -> new TreasureCurios(p.stacksTo(1).fireResistant(), setName, 4)).lang(ch4).simpleModel().build(ModItemTags.CURIOS_TREASURE_4, ModItemTags.CURIOS_TREASURES);
        TREASURE_SETS_MAP.put(setName, this);
        this.unidentifieds = ImmutableMap.of(
                TreasureQuality.COMMON, FlashDays.getSimpleReg().item("unidentified_" + name + "0", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.COMMON)).lang("未鉴定的【"+setNameCn+"】-普通").model(this::generatorModelUnidentified).build(),
                TreasureQuality.UNCOMMON, FlashDays.getSimpleReg().item("unidentified_" + name + "1", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.UNCOMMON)).lang("未鉴定的【"+setNameCn+"】-一般").model(this::generatorModelUnidentified).build(),
                TreasureQuality.RARE, FlashDays.getSimpleReg().item("unidentified_" + name + "2", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.RARE)).lang("未鉴定的【"+setNameCn+"】-稀有").model(this::generatorModelUnidentified).build(),
                TreasureQuality.EPIC, FlashDays.getSimpleReg().item("unidentified_" + name + "3", (p) -> new UnidentifiedTreasure(p,TreasureSets.this,TreasureQuality.EPIC)).lang("未鉴定的【"+setNameCn+"】-史诗").model(this::generatorModelUnidentified).build()
        );
        this.box = FlashDays.getSimpleReg().item("box_of_" + name, (p) -> new TreasureBox(p.stacksTo(64).fireResistant(), setName)).lang("珍宝之匣【" + setNameCn + "】").simpleModel().build();
        FlashDays.getSimpleReg().lang("tip." + this.setName + ".desc", setDesc);
        FlashDays.getSimpleReg().lang("tip." + this.setName + ".desc4", setDesc4);
        FlashDays.getSimpleReg().lang("tip." + this.setName + ".nameDesc", setNameCn);
        this.curiosList = ImmutableList.of(treasure1, treasure2, treasure3, treasure4);
        SetTiger.register(setName, tiger);
    }

    private void generatorModel(Supplier<? extends ItemLike> curios, SimpleItemModelProvider provider) {
        var key = ForgeRegistries.ITEMS.getKey(curios.get().asItem());
        var resource = new ResourceLocation(key.getNamespace(), "item/" + key.getPath().substring(0, key.getPath().length() - 1));
        provider.getBuilder(key.toString())
                .parent(new ModelFile.UncheckedModelFile("item/generated"))
                .texture("layer1", provider.checkTexture(resource) ? resource : new ResourceLocation("flashdays", "item/" + "treasure_curios_default"))
                .texture("layer0", new ResourceLocation("flashdays", "item/" + "treasure_background"))
                .customLoader(ItemLayerModelBuilder::begin)
                .end();
    }
    private void generatorModelUnidentified(Supplier<? extends ItemLike> curios, SimpleItemModelProvider provider) {
        var key = ForgeRegistries.ITEMS.getKey(curios.get().asItem());
        var resource = new ResourceLocation(key.getNamespace(), "item/" + key.getPath().substring(0, key.getPath().length() - 1));
        provider.getBuilder(key.toString())
                .parent(new ModelFile.UncheckedModelFile("item/generated"))
                .texture("layer1", new ResourceLocation( "item/shears"))
                .texture("layer0", new ResourceLocation("flashdays", "item/" + "treasure_background"))
                .customLoader(ItemLayerModelBuilder::begin)
                .end();
    }
    public static TreasureSets create(String name, String ch1, String ch2, String ch3, String ch4, String setName, String setNameCn, String setDesc, String desc4, SetTiger tiger) {
        return new TreasureSets(name, ch1, ch2, ch3, ch4, setName, setNameCn, setDesc, desc4, tiger);
    }

    public static TreasureSets createSimple(String name, String ch1, String ch2, String ch3, String ch4, String setName, String setNameCn, String setDesc, String desc4, SetTiger tiger) {
        return new TreasureSets(name, ch1, ch2, ch3, ch4, setName, setNameCn, setDesc, desc4, tiger, true);
    }

    public Item indexBy(int index) {
        return switch (index) {
            case 0 -> treasure1.get();
            case 1 -> treasure2.get();
            case 2 -> treasure3.get();
            case 3 -> treasure4.get();
            default -> treasure1.get();
        };
    }
}
